package com.framework.auth.util;

import com.framework.common.exception.BusinessException;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zzg
 */
public class RequestUtil {
    public static HttpServletRequest getRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        // 其实这是不可能的， 但是代码扫描会提示，就改一改
        if (ObjectUtils.isEmpty(requestAttributes)) {
            throw new BusinessException("请求错误！");
        }
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        if (ObjectUtils.isEmpty(request)) {
            throw new BusinessException("请求错误！");
        }
        return request;
    }

}